/**
** File Name="treecontrol.cpp"
** Header file for treecontrol
** Created by Frank
** on 2011/08/08
**
**
*/

#include "stdafx.h"
#include "resource.h"
#include "treecontrol.h"


TCHAR JkTreeCtrl::szClassName[]=_T("jk_tree_control");

JkTreeCtrl* JkTreeCtrl::GetJkTreeCtrl(HWND hwnd)
{
	return (JkTreeCtrl *)GetWindowLong(hwnd, 0);
}

void JkTreeCtrl::SetJkTreeCtrl(JkTreeCtrl *ccp)
{
	SetWindowLong(ccp->hwnd, 0, (LONG)ccp);
}

void JkTreeCtrl::RegisterJkTreeCtrl()
{
	WNDCLASSEX wc;
	wc.cbSize = sizeof(wc);
	wc.lpszClassName = JkTreeCtrl::szClassName;
	wc.hInstance = GetModuleHandle(0);
	wc.lpfnWndProc = JkTreeCtrl::WndProc;
	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
	wc.hIcon = 0;
	wc.lpszMenuName = 0;
	wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
	wc.style = 0;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = sizeof( JkTreeCtrl * );
	wc.hIconSm = 0;
	RegisterClassEx(&wc);
}


JkTreeCtrl::JkTreeCtrl(HWND hwnd)
{
	this->hwnd=hwnd;
}


HWND JkTreeCtrl::CreateJkTreeCtrl(HWND hwnd)
{
	//MessageBox(hwnd, _T("Hello 123433 !"), _T("Messaage"), MB_OK);
	return CreateJkTreeCtrl(hwnd, 0, 0, 200, 300);
}

HWND JkTreeCtrl::CreateJkTreeCtrl(HWND hwnd,int x, int y, int w, int h)
{
	HWND hwndCtrl;
	hwndCtrl = CreateWindowEx(
		WS_EX_LEFT, //WS_EX_CLIENTEDGE, // give it a standard border
		JkTreeCtrl::szClassName, JkTreeCtrl::szClassName,
		WS_VISIBLE | WS_CHILD,
		x, y, w, h,
		hwnd, NULL, GetModuleHandle(0), NULL);
	return hwndCtrl;
}

LRESULT CALLBACK JkTreeCtrl::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	// retrieve the custom structure POINTER for THIS window
	JkTreeCtrl *ccp = JkTreeCtrl::GetJkTreeCtrl(hwnd);
	int width, height;
	//MessageBox(hwnd, _T("Hello abcvfsdf !"), _T("Messaage 234"), MB_OK);
	switch(msg)
	{
	case WM_NCCREATE:
		ccp = new JkTreeCtrl(hwnd);
		JkTreeCtrl::SetJkTreeCtrl(ccp);
		return (ccp != NULL);
	case WM_CREATE:
		//MessageBox(hwnd, _T("Hello 123 !"), _T("Messaage 234"), MB_OK);
		ccp->OnCreate(wParam, lParam);
		break;
	case WM_SIZE:
		width = LOWORD(lParam);
		height = HIWORD(lParam);
        MoveWindow(ccp->hTree, 0, 0, width, height, TRUE);
        
	//case WM_PAINT:
		//return ccp->OnPaint(wParam, lParam);

	// Clean up when the window is destroyed.
	case WM_NCDESTROY:
		free(ccp); 
		break;

	case WM_ERASEBKGND:
		return 1;

	//case WM_LBUTTONDOWN:
		//return ccp->OnLButtonDown(wParam, lParam);

	case WM_MOUSEACTIVATE:
		SetFocus(hwnd);
		return MA_ACTIVATE;

	//case WM_SETFONT:
		//return ccp->OnSetFont(wParam, lParam);

	default:
		break;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}


LRESULT JkTreeCtrl::OnCreate(WPARAM wParam, LPARAM lParam)
{
InitCommonControls();       // make our tree control to work
//MessageBox(hwnd, _T("Hello !"), _T("Messaage"), MB_OK);
hTree= CreateWindowEx(WS_EX_CLIENTEDGE, 
					  _T("SysTreeView32"), 
			 _T("SysTreeView32"), WS_VISIBLE | WS_CHILD |TVS_HASBUTTONS | 
                    TVS_HASLINES | TVS_LINESATROOT | TVS_EDITLABELS | 
                    TVS_TRACKSELECT | TVS_SINGLEEXPAND | TVS_INFOTIP | 
                    TVS_FULLROWSELECT | WS_BORDER | WS_TABSTOP, 0,0, 200, 200, 
					hwnd, HMENU(IDC_TREE1), GetModuleHandle(0), NULL);

// creating image list and put it into the tree control
//====================================================//
            hImageList=ImageList_Create(16,16,ILC_COLOR16,2,10);                      // Macro: 16x16:16bit with 2 pics [array]
            hBitMap=LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_TREE));                      // load the picture from the resource
            ImageList_Add(hImageList,hBitMap,NULL);                                   // Macro: Attach the image, to the image list
            DeleteObject(hBitMap);                                                    // no need it after loading the bitmap
            SendMessage(hTree,TVM_SETIMAGELIST,0,(LPARAM)hImageList); // put it onto the tree control
            
            tvinsert.hParent=NULL;          // top most level no need handle
            tvinsert.hInsertAfter=TVI_ROOT; // work as root level
            tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
            tvinsert.item.pszText=_T("Parent");
            tvinsert.item.iImage=0;
            tvinsert.item.iSelectedImage=1;
//          [+]
//           | 
/*           |--*/Parent=(HTREEITEM)SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/*           |  */Root=Parent;
/*           |  */Before=Parent;                   // handle of the before root
/*           |  */tvinsert.hParent=Parent;         // handle of the above data
/*           |  */tvinsert.hInsertAfter=TVI_LAST;  // below parent
/*           |  */tvinsert.item.pszText=_T("Child 1");
/*           |    |--[+] */
/*           |    |   |  */
/*           |    |   |*/ Parent=(HTREEITEM)SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/*           |    |   |*/ tvinsert.hParent=Parent;
/*           |    |   |*/ tvinsert.item.pszText=_T("Child of Child1");
/*           |    |   |*/ Parent=(HTREEITEM)SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
/*           |    |   |-[+]*/
/*           |    |      | */ 
/*           |    |      | */
/*           |    |   */ tvinsert.hParent=Parent;
/*           |    |   */ tvinsert.hInsertAfter=TVI_LAST;
/*           |    |   */ tvinsert.item.pszText=_T("Double Click Me!");
/*           |    |   */ tvinsert.item.mask=TVIF_TEXT;
/*           |    |   */ SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);           
/*           |  */tvinsert.hParent=Before;         // handle of the above data
/*           |  */tvinsert.hInsertAfter=TVI_LAST;  // below parent
/*           |  */tvinsert.item.pszText=_T("Child 2");
/*           |  */Parent=(HTREEITEM)SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);     
/*           |  */
             tvinsert.hParent=NULL;         // top most level no need handle
             tvinsert.hInsertAfter=TVI_LAST; // work as root level
             tvinsert.item.pszText=_T("Parent2");
             Parent=(HTREEITEM)SendMessage(hTree,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
       

	return 1;
}
